package com.sgb.kjwl.repository.impl;

import com.sgb.kjwl.model.api.ApiSource;
import com.sgb.kjwl.model.api.PreferenceSource;
import com.sgb.link.entity.AreaEntity;
import com.sgb.kjwl.model.entity.BannerEntity;
import com.sgb.kjwl.model.entity.BuildEntity;
import com.sgb.kjwl.model.entity.CompanyBuyEntity;
import com.sgb.kjwl.model.entity.CompanyBuyHeader;
import com.sgb.kjwl.model.entity.DeviceEntity;
import com.sgb.kjwl.model.entity.DeviceOutEntity;
import com.sgb.jicai.entity.FilterEntity;
import com.sgb.kjwl.model.entity.InformationEntity;
import com.sgb.kjwl.model.entity.LeaseEntity;
import com.sgb.kjwl.model.entity.MachineInEntity;
import com.sgb.kjwl.model.entity.MachineOutEntity;
import com.sgb.kjwl.model.entity.MaterialListEntity;
import com.sgb.kjwl.model.entity.MaterialMenuEntity;
import com.swgk.core.view.entity.MenuEntity;
import com.sgb.kjwl.model.entity.NewsEntity;
import com.sgb.kjwl.model.entity.PackersBusinessScopeEntity;
import com.sgb.kjwl.model.entity.PackersPersonDetailEntity;
import com.sgb.kjwl.model.entity.PackersPersonEntity;
import com.sgb.kjwl.model.entity.PackersSkillsEntity;
import com.sgb.kjwl.model.entity.PackersTeamEntity;
import com.sgb.kjwl.model.entity.ParentAreaEntity;
import com.sgb.kjwl.model.entity.PickProInfoEntity;
import com.sgb.kjwl.model.entity.ProChildListEntity;
import com.sgb.kjwl.model.entity.ProListEntity;
import com.sgb.kjwl.model.entity.ProvinceEntity;
import com.sgb.link.entity.PublicTypeEntity;
import com.sgb.kjwl.model.entity.RecommendEntity;
import com.sgb.kjwl.model.entity.RentingEntity;
import com.sgb.kjwl.model.entity.SearchDeviceOutEntity;
import com.sgb.kjwl.model.entity.SearchMachineInEntity;
import com.sgb.kjwl.model.entity.SearchMachineOutEntity;
import com.sgb.kjwl.model.entity.SearchMaterialEntity;
import com.sgb.kjwl.model.entity.SupplierEntity;
import com.sgb.kjwl.model.entity.TalentEntity;
import com.sgb.kjwl.model.entity.TenderInfoEntity;
import com.sgb.kjwl.model.entity.supplier3.DictEntity3;
import com.sgb.kjwl.model.entity.supplier3.SupplierRecommendEntity;
import com.sgb.kjwl.model.response.CommonResponse;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.repository.HomeRepertory;
import com.swgk.core.base.api.BaseApiSource;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.preference.BasePreferenceSource;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.ObservableSource;
import io.reactivex.functions.Function;

public class HomeRepertoryImpl extends BaseRepertoryImpl implements HomeRepertory {
    private ApiSource apiSource;
    private PreferenceSource preferenceSource;

    public HomeRepertoryImpl(BaseApiSource apiSource, BasePreferenceSource preferenceSource) {
        super(apiSource, preferenceSource);
        this.apiSource = (ApiSource) apiSource;
        this.preferenceSource = (PreferenceSource) preferenceSource;
    }

    @Override
    public Observable<BaseEntity<List<BannerEntity>>> getMaterialBanner() {
        return apiSource.getMaterialBanner().compose(this.<BaseEntity<List<BannerEntity>>>rxSchedulerHelper());
    }

    @Override
    public Observable<BaseEntity<List<MenuEntity>>> getMenu() {
        return apiSource.getMenu().compose(this.<BaseEntity<List<MenuEntity>>>rxSchedulerHelper());
    }

    @Override
    public Observable<BaseEntity<List<MaterialMenuEntity>>> getMaterialMenu() {
        return apiSource.getMaterialMenu().compose(this.<BaseEntity<List<MaterialMenuEntity>>>rxSchedulerHelper());
    }

    @Override
    public Observable<ListResponse<List<MaterialListEntity>>> getMaterialList(String cid, String areacode) {
        return apiSource.getMaterialList(cid, areacode).compose(this.<ListResponse<List<MaterialListEntity>>>rxSchedulerHelper());
    }

    @Override
    public Observable<BaseEntity<List<CommonResponse<SupplierEntity>>>> getSupplierList(String areacode) {
        return apiSource.getSupplierList(areacode).compose(this.<BaseEntity<List<CommonResponse<SupplierEntity>>>>rxSchedulerHelper());
    }

    @Override
    public Observable<BaseEntity<List<CommonResponse<InformationEntity>>>> getInformation(String areacode) {
        return apiSource.getInformation(areacode).compose(this.<BaseEntity<List<CommonResponse<InformationEntity>>>>rxSchedulerHelper());
    }

    @Override
    public Observable<BaseEntity<NewsEntity>> getNewsDetail(String id) {
        return apiSource.getNewsDetail(id).compose(this.<BaseEntity<NewsEntity>>rxSchedulerHelper());
    }

    @Override
    public Observable<List<String>> getMaterialHotSearch() {
        return apiSource.getMaterialHotSearch().compose(rxSchedulerHelper());
    }

    @Override
    public Observable<Boolean> saveSearchHistory(String name, int index) {
        return Observable.create(new ObservableOnSubscribe<Boolean>() {
            @Override
            public void subscribe(ObservableEmitter<Boolean> e) throws Exception {

                e.onNext(true);
            }
        }).compose(rxSchedulerHelper());
    }

    @Override
    public Observable<List<String>> querySearchHistory(int index) {
        return Observable.create(new ObservableOnSubscribe<List<String>>() {
            @Override
            public void subscribe(ObservableEmitter<List<String>> e) throws Exception {
                List<String> list = new ArrayList<>();

                e.onNext(list);
            }
        }).compose(rxSchedulerHelper());
    }

    @Override
    public Observable<Boolean> delAllSearchHistory(int index) {
        return Observable.create(new ObservableOnSubscribe<Boolean>() {
            @Override
            public void subscribe(ObservableEmitter<Boolean> e) throws Exception {

                e.onNext(true);
            }
        }).compose(rxSchedulerHelper());
    }

    @Override
    public Observable<Boolean> delSearchHistory(String name, int index) {
        return Observable.create(new ObservableOnSubscribe<Boolean>() {
            @Override
            public void subscribe(ObservableEmitter<Boolean> e) throws Exception {

                e.onNext(true);
            }
        }).compose(rxSchedulerHelper());
    }

    @Override
    public Observable<BaseEntity<List<BannerEntity>>> getMachineBanner() {
        return apiSource.getMachineBanner().compose(this.<BaseEntity<List<BannerEntity>>>rxSchedulerHelper());
    }

    @Override
    public Observable<BaseEntity<List<MenuEntity>>> getMachineMenu() {
        return apiSource.getMachineMenu().compose(this.<BaseEntity<List<MenuEntity>>>rxSchedulerHelper());
    }

    @Override
    public Observable<BaseEntity<List<LeaseEntity>>> getMachineLease(String areacode) {
        return apiSource.getMachineLease(areacode).compose(this.<BaseEntity<List<LeaseEntity>>>rxSchedulerHelper());
    }

    @Override
    public Observable<BaseEntity<List<RentingEntity>>> getMachineRenting(String areacode) {
        return apiSource.getMachineRenting(areacode).compose(this.<BaseEntity<List<RentingEntity>>>rxSchedulerHelper());
    }

    @Override
    public Observable<ListResponse<List<SearchMaterialEntity>>> getMaterialSearch(String areacode, String item_provinceId, String key, int page, int size) {
        return apiSource.getMaterialSearch(areacode, item_provinceId, key, page, size).compose(rxSchedulerHelper());
    }

    public Observable<List<String>> getMachineHotSearch() {
        return apiSource.getMachineHotSearch().compose(rxSchedulerHelper());
    }

    @Override
    public Observable<BaseEntity<ListResponse<List<SearchMachineOutEntity>>>> getMachineSearch(int page, int size, String areacode, String key) {
        return apiSource.getMachineSearch(page, size, areacode, key).compose(rxSchedulerHelper());
    }

    @Override
    public Observable<BaseEntity<ListResponse<List<SearchMachineInEntity>>>> getMachineInSearch(int page, int size, String areacode, String key) {
        return apiSource.getMachineInSearch(page, size, areacode, key).compose(rxSchedulerHelper());
    }

    @Override
    public Observable<BaseEntity<List<BannerEntity>>> getDeviceBanner() {
        return apiSource.getDeviceBanner().compose(this.<BaseEntity<List<BannerEntity>>>rxSchedulerHelper());
    }

    @Override
    public Observable<BaseEntity<List<MenuEntity>>> getDeviceMenu() {
        return apiSource.getDeviceMenu().compose(this.<BaseEntity<List<MenuEntity>>>rxSchedulerHelper());
    }

    @Override
    public Observable<BaseEntity<List<DeviceEntity>>> getDeviceRecommand(String areacode) {
        return apiSource.getDeviceRecommand(areacode).compose(this.<BaseEntity<List<DeviceEntity>>>rxSchedulerHelper());
    }

    @Override
    public Observable<BaseEntity<List<FilterEntity<List<FilterEntity>>>>> getDeviceFilter() {
        return apiSource.getDeviceFilter().compose(this.<BaseEntity<List<FilterEntity<List<FilterEntity>>>>>rxSchedulerHelper());
    }

    @Override
    public Observable<BaseEntity<List<FilterEntity<List<FilterEntity>>>>> getDeviceFilterByNumber(String pNo, String cNo) {
        return apiSource.getDeviceFilterByNumber(pNo, cNo).compose(this.<BaseEntity<List<FilterEntity<List<FilterEntity>>>>>rxSchedulerHelper());
    }

    @Override
    public Observable<List<String>> getDeviceHotSearch() {
        return apiSource.getDeviceHotSearch().compose(rxSchedulerHelper());
    }

    @Override
    public Observable<ListResponse<List<SearchDeviceOutEntity>>> getDeviceOutSearch(int page, int size, String areacode, String key) {
        return apiSource.getDeviceOutSearch(page, size, areacode, key).compose(rxSchedulerHelper());
    }

    @Override
    public Observable<BaseEntity<List<BannerEntity>>> getPackersBanner() {
        return apiSource.getPackersBanner().compose(this.<BaseEntity<List<BannerEntity>>>rxSchedulerHelper());
    }

    @Override
    public Observable<BaseEntity<List<MenuEntity>>> getPackersMenu() {
        return apiSource.getPackersMenu().compose(this.<BaseEntity<List<MenuEntity>>>rxSchedulerHelper());
    }

    @Override
    public Observable<BaseEntity<List<BuildEntity>>> getPackersBuild(String areacode) {
        return apiSource.getPackersBuild(areacode).compose(this.<BaseEntity<List<BuildEntity>>>rxSchedulerHelper());
    }

    @Override
    public Observable<BaseEntity<List<TalentEntity>>> getPackersTalent(String areacode) {
        return apiSource.getPackersTalent(areacode).compose(this.<BaseEntity<List<TalentEntity>>>rxSchedulerHelper());
    }

    @Override
    public Observable<BaseEntity<PackersPersonDetailEntity>> getPackersPersonDetailEntity(String id) {
        return apiSource.getPackersPersonDetailEntity(id).compose(this.<BaseEntity<PackersPersonDetailEntity>>rxSchedulerHelper());
    }

    @Override
    public Observable<BaseEntity<PackersSkillsEntity>> getPackersSkillsEntity(String id) {
        return apiSource.getPackersSkillsEntity(id).compose(this.<BaseEntity<PackersSkillsEntity>>rxSchedulerHelper());
    }

    @Override
    public Observable<BaseEntity<PackersBusinessScopeEntity>> getPackersBusinessScopeEntity(String id) {
        return apiSource.getPackersBusinessScopeEntity(id).compose(this.<BaseEntity<PackersBusinessScopeEntity>>rxSchedulerHelper());
    }

    @Override
    public Observable<BaseEntity<ListResponse<List<AreaEntity>>>> getAreaList() {
        return apiSource.getAreaList().flatMap(new Function<BaseEntity<ListResponse<List<AreaEntity>>>, ObservableSource<? extends BaseEntity<ListResponse<List<AreaEntity>>>>>() {
            @Override
            public ObservableSource<? extends BaseEntity<ListResponse<List<AreaEntity>>>> apply(BaseEntity<ListResponse<List<AreaEntity>>> listResponseBaseEntity) throws Exception {
                List<AreaEntity> list = listResponseBaseEntity.getData().getList();
                AreaEntity areaEntity = new AreaEntity();
                areaEntity.setSimpleName("全部");
                areaEntity.setNo("");
                list.add(0, areaEntity);
                return Observable.just(listResponseBaseEntity);
            }
        }).compose(this.<BaseEntity<ListResponse<List<AreaEntity>>>>rxSchedulerHelper());
    }

    @Override
    public Observable<BaseEntity<ListResponse<List<CompanyBuyHeader<CompanyBuyEntity>>>>> getCompanyBuy(Map map, int page, int size) {
        return apiSource.getCompanyBuy(map, page, size).compose(this.<BaseEntity<ListResponse<List<CompanyBuyHeader<CompanyBuyEntity>>>>>rxSchedulerHelper());
    }

    @Override
    public Observable<BaseEntity<PickProInfoEntity<ProListEntity<List<ProChildListEntity>>>>> companyProject(String compNO) {
        return apiSource.companyProject(compNO).compose(this.<BaseEntity<PickProInfoEntity<ProListEntity<List<ProChildListEntity>>>>>rxSchedulerHelper());
    }

    @Override
    public Observable<BaseEntity<TenderInfoEntity>> getHotTenderInfo(String no) {
        return apiSource.getHotTenderInfo(no).compose(this.<BaseEntity<TenderInfoEntity>>rxSchedulerHelper());
    }

    @Override
    public Observable<BaseEntity<List<ParentAreaEntity<List<ParentAreaEntity>>>>> getMachineArea(int areacode) {
        return apiSource.getMachineArea(areacode).compose(this.<BaseEntity<List<ParentAreaEntity<List<ParentAreaEntity>>>>>rxSchedulerHelper());
    }

    @Override
    public Observable<BaseEntity<ListResponse<List<MachineOutEntity>>>> getMachineOut(int page, int size, int priceSort, Map map) {
        return apiSource.getMachineOut(page, size, priceSort, map).compose(this.<BaseEntity<ListResponse<List<MachineOutEntity>>>>rxSchedulerHelper());
    }

    @Override
    public Observable<BaseEntity<List<PublicTypeEntity>>> getMachineType() {
        return apiSource.getMachineType().compose(this.<BaseEntity<List<PublicTypeEntity>>>rxSchedulerHelper());
    }

    @Override
    public Observable<BaseEntity<List<FilterEntity<List<FilterEntity>>>>> getMachineFilter() {
        return apiSource.getMachineFilter().compose(this.<BaseEntity<List<FilterEntity<List<FilterEntity>>>>>rxSchedulerHelper());
    }

    @Override
    public Observable<BaseEntity<List<FilterEntity<List<FilterEntity>>>>> getMachineFilterInfo(String categoryNo, String typeNo, int areacode) {
        return apiSource.getMachineFilterInfo(categoryNo, typeNo, areacode).compose(this.<BaseEntity<List<FilterEntity<List<FilterEntity>>>>>rxSchedulerHelper());
    }

    @Override
    public Observable<BaseEntity<ListResponse<List<MachineInEntity>>>> getMachineIn(int page, int size, int priceSort, Map map) {
        return apiSource.getMachineIn(page, size, priceSort, map).compose(this.<BaseEntity<ListResponse<List<MachineInEntity>>>>rxSchedulerHelper());
    }

    @Override
    public Observable<ListResponse<List<DeviceOutEntity>>> getDeviceOut(Map map, int page, int limit, int priceSort) {
        return apiSource.getDeviceOut(map, page, limit, priceSort).compose(this.<ListResponse<List<DeviceOutEntity>>>rxSchedulerHelper());
    }

    @Override
    public Observable<BaseEntity<List<ParentAreaEntity<List<ParentAreaEntity>>>>> getDeviceArea(int areaId) {
        return apiSource.getDeviceArea(areaId).compose(this.<BaseEntity<List<ParentAreaEntity<List<ParentAreaEntity>>>>>rxSchedulerHelper());
    }

    @Override
    public Observable<BaseEntity<List<ProvinceEntity<List<ProvinceEntity>>>>> getAllProvince() {
        return apiSource.getAllProvince().compose(this.<BaseEntity<List<ProvinceEntity<List<ProvinceEntity>>>>>rxSchedulerHelper());
    }

    @Override
    public Observable<BaseEntity<ListResponse<List<PackersPersonEntity>>>> getPackersPerson(int page, int limit, Map map) {
        return apiSource.getPackersPerson(page, limit, map).compose(this.<BaseEntity<ListResponse<List<PackersPersonEntity>>>>rxSchedulerHelper());
    }

    @Override
    public Observable<BaseEntity<ListResponse<List<PackersTeamEntity>>>> getPackersTeam(int page, int limit, Map map) {
        return apiSource.getPackersTeam(page, limit, map).compose(this.<BaseEntity<ListResponse<List<PackersTeamEntity>>>>rxSchedulerHelper());
    }

    @Override
    public Observable<BaseEntity> checkToken(Map map) {
        return apiSource.checkToken(map).compose(rxSchedulerHelper());
    }

    @Override
    public Observable<BaseEntity<List<BannerEntity>>> getHomeBanner() {
        return apiSource.getHomeBanner().compose(rxSchedulerHelper());
    }

    @Override
    public Observable<BaseEntity<List<MenuEntity>>> getHomeMenu() {
        return apiSource.getHomeMenu().compose(rxSchedulerHelper());
    }

    @Override
    public Observable<BaseEntity<List<MaterialMenuEntity>>> getHomeRecommendType(String areacode) {
        return apiSource.getHomeRecommendType(areacode).compose(rxSchedulerHelper());
    }

    @Override
    public Observable<BaseEntity<List<RecommendEntity>>> getHomeRecommandList(String areacode, String type) {
        return apiSource.getHomeRecommandList(areacode, type).compose(rxSchedulerHelper());
    }

    @Override
    public Observable<BaseEntity<ListResponse<List<SupplierRecommendEntity>>>> getSupplierMapData(Map map) {
        return apiSource.getSupplierMapData(map).compose(rxSchedulerHelper());
    }

    @Override
    public Observable<BaseEntity<ListResponse<List<SupplierRecommendEntity>>>> getGoodsMapData(Map map) {
        return apiSource.getGoodsMapData(map).compose(rxSchedulerHelper());
    }

    @Override
    public Observable<BaseEntity<List<DictEntity3>>> getSupplierCityData(String areacode) {
        return apiSource.getSupplierCityData(areacode).compose(rxSchedulerHelper());
    }

    @Override
    public Observable<BaseEntity<List<DictEntity3>>> getSupplierDistanceData() {
        return apiSource.getSupplierDistanceData().compose(rxSchedulerHelper());
    }

    public Observable<BaseEntity<List<DictEntity3>>> getDistance() {
        return apiSource.getDistance().compose(rxSchedulerHelper());
    }

    @Override
    public Observable<BaseEntity<List<DictEntity3>>> getSupplierSortData() {
        return apiSource.getSupplierSortData().compose(rxSchedulerHelper());
    }

    @Override
    public Observable<BaseEntity<ListResponse<List<SupplierRecommendEntity>>>> getSupplierList(Map map) {
        return apiSource.getSupplierList(map).compose(rxSchedulerHelper());
    }

    @Override
    public Observable<BaseEntity<ListResponse<List<SupplierRecommendEntity>>>> getGoodsList(Map map) {
        return apiSource.getGoodsList(map).compose(rxSchedulerHelper());
    }

//    @Override
//    public Observable<BaseEntity<ListResponse<List<SearchSTEntity>>>> getSTList(MaterialFilterJson materialFilterJson) {
//        return apiSource.getSTList(materialFilterJson).compose(rxSchedulerHelper());
//    }

}
